# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

from urllib import parse

import pymongo
# useful for handling different item types with a single interface
from scrapy import Item
from scrapy.exceptions import DropItem


class FirstScrapyPipeline:
    exchange_rate = 8.5309

    def process_item(self, item, spider):
        price = float(item['price'][1:]) * self.exchange_rate
        item['price'] = '￥%.2f' % price
        return item


class DuplicatesPipeline(object):

    def __init__(self):
        self.book_set = set()

    def process_item(self, item, spider):
        name = item['name']
        if name in self.book_set:
            raise DropItem("Duplicate book fund: %s" % item)

        self.book_set.add(name)
        return item


class MongoDBPipeline(object):
    passwd = "mongo18755172427.ck"
    passwd = parse.quote(passwd)  # 对密码先进行编码
    DB_URI = 'mongodb://mongo:' + passwd + '@47.111.153.32:27017'
    DB_NAME = 'scrapy_data'

    def open_spider(self, spider):
        self.client = pymongo.MongoClient(self.DB_URI)
        self.db = self.client[self.DB_NAME]

    def close_spider(self, spider):
        self.client.close()

    def process_item(self, item, spider):
        collection = self.db[self.DB_NAME]
        post = dict(item) if isinstance(item, Item) else item
        collection.insert_one(post)
        return item
